package com.xsky.logic.knowledge;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import junit.framework.TestCase;

import org.codehaus.xfire.client.Client;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

import com.xsky.common.util.XMLConverter;
import com.xsky.operation.vo.TabKnowledgeBase;

public class KnowledgeLogicTest extends TestCase {

	TabKnowledgeBase know;

	KnowledgeLogic logic = new KnowledgeLogic();

	String xml;

	@Override
	protected void setUp() throws Exception {
		know = new TabKnowledgeBase();
		know.setCaseTypeId("1");
		know.setCaseDesc("不需要解决");
		know.setCaseTypeName("分类1");
		know.setCode("code1");
		know.setCreateTime(new Date());
		know.setCreatorId("createdID");
		know.setEngineerEmail("caoshun@gmail.com");
		know.setEngineerJn("aaaa");
		know.setLevel(1);
		know.setWay("s");
		xml = XMLConverter.convertToXML(know);
	}

	public void testAll() throws DocumentException {
		// test add
		String result = logic.add(xml);
		Document document = DocumentHelper.parseText(result);
		String uuid = document.selectSingleNode("//data//uuid").getText();

		assertNotNull(uuid);
		assertEquals(uuid.length(), 32);

		know.setId(uuid);
		// test query
		String resultXML = logic.queryById(uuid);
		TabKnowledgeBase newKnow = XMLConverter.convertToObject(resultXML, TabKnowledgeBase.class);

		assertEquals(newKnow.getId(), uuid);
		assertEquals(newKnow.getCaseTypeName(), know.getCaseTypeName());
		assertEquals(newKnow.getEngineerEmail(), know.getEngineerEmail());
		assertEquals(newKnow.getCreatorId(), know.getCreatorId());

		// test update
		newKnow.setCaseDesc("AAAAAAAAAAA");
		logic.update(XMLConverter.convertToXML(newKnow));
		resultXML = logic.queryById(uuid);
		newKnow = XMLConverter.convertToObject(resultXML, TabKnowledgeBase.class);
		assertEquals(newKnow.getCaseDesc(), "AAAAAAAAAAA");

		// test delete
		result = logic.delete(uuid);
		assertEquals(result.contains("ok"), true);

		resultXML = logic.queryById(uuid);
		assertNull(resultXML);
	}

	public void testQueryByCondition() {
		addKnow(20);
		String xml = "<data><caseTypeId>1</caseTypeId><keyword>解决</keyword><pageNum>4</pageNum><numPerPage>4</numPerPage></data>";
		String returnXML = logic.queryByCondition(xml);
		System.out.println(returnXML);
	}

	public void testQueryHistory() {
		String xml = logic.queryHistory(know.getId());
		System.out.println(xml);
	}

	private void addKnow(int num) {
		for (int i = 0; i < num; i++) {
			logic.add(xml);
		}
	}


}
